package com.ereport.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ereport.entity.Expenses;
import com.ereport.entity.LocalExpenses;
import com.ereport.entity.MblExpenses;
import com.ereport.entity.OutStationExpenses;
import com.ereport.util.ApplicationContext;

public class OsExpensesDAO {
	static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}

	public int insert(Expenses oe) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(oe);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public void deletTrExpenses(int id, java.sql.Date date) throws Exception {
		Session session=startOperations();
		String qtr="delete from OsExpenses where exe_id=:id and osdate=:m";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id", id);	
		qry.setDate("m", date);	
		qry.executeUpdate();
		endOperations(session);
		return ;
		
	}

	public Expenses getDetails(int exeId, Date dt) throws Exception {
		Session session=startOperations();
		String qtr=" from Expenses where exe_id=:id and expdate=:m";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id", exeId);	
		qry.setDate("m", dt);	
		Object obj=qry.uniqueResult();
		Expenses te=(Expenses)obj;
		endOperations(session);
		return te;
	}
	public Expenses getMbl(int id, Date det) throws Exception {
		Session session = startOperations();
		String qrystr="From Expenses where exe_id=:id and expdate=:mdate";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		qr.setDate("mdate", det);
		Expenses adp=(Expenses) qr.uniqueResult();
		endOperations(session);
		return adp;
	}

	public int mblsave(MblExpenses mblExpenses) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(mblExpenses);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public int outsave(OutStationExpenses outStationExpenses) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(outStationExpenses);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public int localsave(LocalExpenses localExpenses) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(localExpenses);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public List<LocalExpenses> getLocalList(int id) throws Exception {
		Session session = startOperations();
		String qrystr="From LocalExpenses where exe_id=:id";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		List<LocalExpenses> adp=(List<LocalExpenses>) qr.list();
		endOperations(session);
		return adp;
	}

	public List<OutStationExpenses> getOutStationList(int id) throws Exception {
		Session session = startOperations();
		String qrystr="From OutStationExpenses where exe_id=:id";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		List<OutStationExpenses> adp=(List<OutStationExpenses>) qr.list();
		endOperations(session);
		return adp;
	}

	public List<MblExpenses> getMblList(int id) throws Exception {
		Session session = startOperations();
		String qrystr="From MblExpenses where exe_id=:id";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		List<MblExpenses> adp=(List<MblExpenses>) qr.list();
		endOperations(session);
		return adp;
	}

	
}
